#include "CallCommand.h"
//
void CallCommand::processCommand(ProcessArg* arg) {
    std::vector<tstring>& response = arg->getSkypeResponse();
    long skypeId = _tstol(response[2].c_str());
    List<tstring>& rawResponse = arg->getSkypeCommWindow().getRawResponse();
    for(;;) {
        rawResponse.lock();
        bool empty = rawResponse.empty();
        if (empty)
            rawResponse.unlock();
        if (!empty || !_needTerminate || rawResponse.wait(RESP_TIMEOUT)) {
            if (empty)
                rawResponse.lock();
            List<tstring>::iterator itr = rawResponse.begin();
            List<tstring>::iterator end = rawResponse.end();
            for (; itr != end; ++itr) {
                _tprintf(TEXT("[%s]\n"), itr->c_str());
                std::vector<tstring> parts;
                int delta = *(itr->begin()) == TEXT('#') ? 1 : 0;
                split(*itr, parts);
                if (_tstol(parts[1 + delta].c_str()) == skypeId) {
                    _processCommandParts(parts, delta);
                }
            }
            rawResponse.clear();
            rawResponse.unlock();
        }
    }
}
//
void CallCommand::_processCommandParts(std::vector<tstring>& parts, int delta) {
    if ((int)parts.size() > 2 + delta) {
        if (parts[2 + delta] == TEXT("DURATION")) {
        //parts = [4]("CALL","15181","DURATION","6")
        _duration = _tstoi(parts[3 + delta].c_str());
        } if (parts[2 + delta] == TEXT("STATUS")) {
            // [5]("#1281259558","CALL","15949","STATUS","UNPLACED")
            /*
            UNPLACED - call was never placed
            ROUTING - call is currently being routed
            EARLYMEDIA - with pstn it is possible that before a call is established, early media is played. For example it can be a calling tone or a waiting message such as all operators are busy.
            FAILED - call failed - try to get a FAILUREREASON for more information.
            RINGING - currently ringing
            INPROGRESS - call is in progress
            ONHOLD - call is placed on hold
            FINISHED - call is finished
            MISSED - call was missed
            REFUSED - call was refused
            BUSY - destination was busy
            CANCELLED (Protocol 2)
            TRANSFERRING - Refer to ALTER CALL TRANSFER command. Added in protocol 7 (API version 3.0)
            TRANSFERRED - Refer to ALTER CALL TRANSFER command. Added in protocol 7 (API version 3.0)
            VM_BUFFERING_GREETING - voicemail greeting is being downloaded
            VM_PLAYING_GREETING - voicemail greeting is being played
            VM_RECORDING - voicemail is being recorded
            VM_UPLOADING - voicemail recording is finished and uploaded into server
            VM_SENT - voicemail has successfully been sent
            VM_CANCELLED - leaving voicemail has been cancelled
            VM_FAILED - leaving voicemail failed; check FAILUREREASON
            WAITING_REDIAL_COMMAND - This status is set when your outgoing call to PSTN gets rejected by remote party. This state was added in version 3.5 (protocol 8).
            REDIAL_PENDING - This status is set when you press redial button on the Call Phones tab of the Skype interface. This state was added in version 3.5 (protocol 8). 
            */
            if (parts[3 + delta] == TEXT("FAILED")) {
                _needTerminate = true;
            } else if (parts[3 + delta] == TEXT("FINISHED")) {
                _needTerminate = true;
            }
            _tprintf(TEXT("*"));
        }
    }

}
